package com.edt.ichannel.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.dao.SendTemplateDao;
import com.edt.ichannel.api.entiy.local.*;
import com.edt.ichannel.api.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 发送短信接口
 *
 * @author 宋寒
 * 2018/7/5 13:18
 */
@Service
public class SendTemplateServiceImpl implements SendTemplateService {
    @Resource
    private SendTemplateDao sendTemplateDao;
    @Resource
    private GGetwayChannelService gGetwayChannelService;
    @Resource
    private BasePhoneOperatorService basePhoneOperatorService;
    @Resource
    private DueOutService dueOutService;
    @Resource
    private DownlinkSendService downlinkSendService;
    @Resource
    private SystemManagementService systemManagementService;

    @Resource
    private EdtApiService edtApiService;

    private final static String CHINA_MOBILE = "2";
    private final static int ENABLE = 1;
    @Resource
    private HttpServletRequest httpServletRequest;

    /**
     * 发送短信接口
     * 帐号（网关通道管理的用户名及密码）、密码（MD5）、发送号码、短信内容
     * 大体接口流程：
     * 1.验证入参判断用户名密码
     * 2.验证手机号所属运营商    移动号码验证 网关状态  若网关关闭 返回信息
     * 3.验证短信模板  若通过   电信、联通 走能力平台接口 直接发送  记录插入到下行 发送记录表 。
     * 移动  把对应信息 插入到待发送 表中    不通过  返回信息
     * 流程结束
     *
     * @param loginName,loginPassword,phone,contents loginName,loginPassword,phone,contents
     * @author 宋寒
     * 2018/7/10 14:12
     */
    @Override
    public ActionResult sendMessages(String loginName, String loginPassword, String phone, String contents) {
        ActionResult actionResult = new ActionResult();
        //根据传过来的参数中的用户名和密码获取对象
        GGetwayChannel gGetwayChannel = gGetwayChannelService.getGGetwayChannelByLoginNameAndLoginPassword(loginName, loginPassword);
        //根据用户名和密码获取接入系统 供存入记录使用
        SystemManagement systemManagement = systemManagementService.getSystemManagementByCondition(loginName, loginPassword);
        if (systemManagement == null) {
            actionResult.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
            actionResult.setMessage("未找到接入系统信息！");
            return actionResult;
        }
        if (gGetwayChannel != null) {
            //判断手机号所属运营商
            String prefix = phone.substring(0, 3);
            BasePhoneOperator basePhoneOperator = basePhoneOperatorService.getBasePhoneOperatorByCondition(prefix);
            //根据手机号前缀判断属于哪一个运营商  所属运营商 1=联通 2=移动 3=电信
            if (basePhoneOperator != null) {
                //移动手机号码需要判断网关通道的状态 其余不需要
                if (CHINA_MOBILE.equals(basePhoneOperator.getOperator())) {
                    if (ENABLE == gGetwayChannel.getChannelState()) {
                        //判断当前短信内容是否匹配上发送模板
                        if (getCountSendTemplateByCondition(contents)) {
                            //存入一条记录到待发表中
                            DueOut dueOut = new DueOut();
                            dueOut.setMessageText(contents);
                            dueOut.setPhoneNumber(phone);
                            dueOut.setOperator(basePhoneOperator.getOperator());
                            dueOut.setSendPassageWay(gGetwayChannel.getChannelName());
                            dueOut.setAccessSystem(systemManagement.getManagementName());
                            dueOutService.saveDueOut(dueOut);
                            actionResult.setSuccess(true);
                            actionResult.setMessage("发送成功");
                        } else {
                            actionResult.setSuccess(false);
                            actionResult.setMessage("短信内容和发送模板没有匹配上");
                        }
                    } else {
                        actionResult.setSuccess(false);
                        actionResult.setMessage("当前网关通道属于停用状态");
                    }
                } else {
                    //联通和电信不要判断网关通道是否关闭直接走下一步 判断短信内容是否匹配到发送模板
                    if (getCountSendTemplateByCondition(contents)) {
                        //匹配成功走能力平台发送短信 然后存入一条记录到下行发送记录表
                        SendSMS sendSMS = new SendSMS();
                        sendSMS.setContent(contents);
                        sendSMS.setReceivePhone(phone);
                        //发送短信
                        ActionResult sendSmsResult = edtApiService.sendsms(sendSMS);
                        DownlinkSend downlinkSend = new DownlinkSend();
                        if (sendSmsResult.isSuccess()) {
                            downlinkSend.setState("1");
                        } else {
                            downlinkSend.setState("0");
                        }
                        downlinkSend.setMessageText(contents);
                        downlinkSend.setPhoneNumber(phone);
                        downlinkSend.setOperator(basePhoneOperator.getOperator());
                        downlinkSend.setSendPassageWay(gGetwayChannel.getChannelName());
                        downlinkSend.setAccessSystem(systemManagement.getManagementName());
                        //保存记录到下行发送记录表
                        downlinkSendService.saveDownlinkSend(downlinkSend);
                        actionResult = sendSmsResult;
                    } else {
                        actionResult.setSuccess(false);
                        actionResult.setMessage("短信内容和发送模板没有匹配上");
                    }
                }
            } else {
                actionResult.setSuccess(false);
                actionResult.setMessage("当前手机号没有匹配出所属的运营商");
            }
        } else {
            actionResult.setSuccess(false);
            actionResult.setMessage("您输入的用户名和密码没有匹配到对应的网关通道");
        }
        return actionResult;
    }

    @Override
    public SendMessage checkParam(String json) {
        SendMessage sendMessage = new SendMessage();
        try {
            sendMessage = JSON.parseObject(json, SendMessage.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sendMessage;

    }


    @Override
    public boolean getCountSendTemplateByCondition(String contents) {
        int matchResult = sendTemplateDao.getCountSendTemplateByCondition(contents);
        if (matchResult > 0) {
            return true;
        }
        return false;
    }
}
